﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Globalization;
using System.Threading;
using System.Xml;

namespace botliga
{
    class Program
    {
        static void Main(string[] args)
        {
            CultureInfo ci;
            RegionInfo ri;
            TimeSpan ts;
            ci = CultureInfo.CurrentCulture;
            ri = RegionInfo.CurrentRegion;
            ts = DateTime.Now - DateTime.Now;
            List<CoreSlave> Esclavos = new List<CoreSlave>();
           
            StreamWriter sm = new StreamWriter( Directory.GetCurrentDirectory()+@"\log\" + DateTime.Now.ToShortDateString().Replace('/', '_') + "_" + DateTime.Now.ToShortTimeString().Replace(':', '_').Replace('.', '_') + ".log");
            Esclavos = new List<CoreSlave>();


            if (!Directory.Exists(Directory.GetCurrentDirectory() + "\\log"))
            {
                Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\log");
            }
            StreamReader sr = new StreamReader(Directory.GetCurrentDirectory() + "\\mysqlcon.txt");
            sr.ReadLine();
            CoreBase core = new CoreMaster("FFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFF",
            ri.ThreeLetterISORegionName, ri.DisplayName, (uint)ci.LCID, "DOTALEAGUE", sr.ReadLine(),
            "dotaleague", "DicE", '*', true, true, false, 0x1A, new List<byte>(4), new List<byte>(4),
            "pvpgn", "ombu", (uint)200, (uint)1, sm, Esclavos);
            int contador = 0;
            try
            {
                core.Iniciar();
                foreach (var escl in Esclavos)
                {
                    escl.Iniciar();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Lost Connection - En iniciar");
                sm.WriteLine("["+DateTime.Now.ToString()+"]"+e.Message);
                sm.WriteLine("["+DateTime.Now.ToString()+"]"+"Lost Connection - En iniciar");
            }
            while (true)
            {

                if (core.Isconnected)
                {
                    try
                    {
                        core.ExtractPackets();
                        core.ProcessPackets();
                        core.update();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        sm.WriteLine("["+DateTime.Now.ToString()+"]"+e.Message);
                        Console.WriteLine("Lost Connection - En Process,Extract,Update - Master");
                        sm.WriteLine("["+DateTime.Now.ToString()+"]"+"Lost Connection - En Process,Extract,Update - Master");
                    }

                }
                else
                {
                    Thread.Sleep(20000);
                    try
                    {
                        core.Reconnect();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        sm.WriteLine("["+DateTime.Now.ToString()+"]"+e.Message);
                        Console.WriteLine("Lost Connection - En reconect - Master");
                        sm.WriteLine("["+DateTime.Now.ToString()+"]"+"Lost Connection - En reconect - Master");
                    }
                    contador++;
                    if (contador == 100)
                    {
                        Console.WriteLine("Exiting..");
                        sm.WriteLine("["+DateTime.Now.ToString()+"]"+"Exiting..");
                        break;
                    }
                }

                foreach (var escl in Esclavos)
                {
                    if (escl.Isconnected)
                    {
                        try
                        {
                            escl.ProcessPackets();
                            escl.ExtractPackets();
                            escl.update();
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                            sm.WriteLine("["+DateTime.Now.ToString()+"]"+e.Message);
                            Console.WriteLine("Lost Connection - En Process,Extract,Update - Esclavo");
                            sm.WriteLine("["+DateTime.Now.ToString()+"]"+"Lost Connection - En Process,Extract,Update - Esclavo");
                        }

                    }
                    else
                    {
                        Thread.Sleep(20000);
                        try
                        {
                            escl.Reconnect();
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                            sm.WriteLine("["+DateTime.Now.ToString()+"]"+e.Message);
                            Console.WriteLine("Lost Connection - En reconect - Esclavo");
                            sm.WriteLine("["+DateTime.Now.ToString()+"]"+"Lost Connection - En reconect - Esclavo");
                        }

                    }
                }

            }
            sm.Close();
            Console.ReadKey();
        }
    }
}
